<?php

abstract class My_Controller_ActionEvent extends My_Controller_Action {

    protected function processEventsByInfoType() {
        $infoTypes = My_Utils::getEventInfoTypes();
        $type = $this->_getParam('type');
        $limit = 5;
        if (empty($type) || (is_string($type) && !isset($infoTypes[$type]))) {
            $type = null;
            $this->view->showAllLink = true;
        } else {
            $infoTypesTmp = array();
            if(is_array($type)) {
                foreach ($type as $val) {
                    $infoTypesTmp[$val] = $infoTypes[$val];
                }
            } else {
                $infoTypesTmp[$type] = $infoTypes[$type];
            }
            $infoTypes = $infoTypesTmp;
            $limit = 50;
            $this->view->showAllLink = false;
        }
        list($startDate, $endDate) = $this->getStartAndEndDates();
        $list = $this->getEventListByEventTypes($infoTypes, $startDate, $endDate, $limit);
        $this->view->list = $list;
    }

    protected function getEventListByEventTypes($infoTypes, $startDate, $endDate, $limit = 10) {
        $daoEvent = new Application_Model_DAO_Event();
        $list = array();
        $emptyList = array();
        foreach ($infoTypes as $key => $val) {
            $tmp = array('type' => $key, 'display' => $this->translate($val));
            $select = $daoEvent->getAllEvents();
            $daoEvent->limitEventsByInfoType($select, (array) $key);
            $daoEvent->limitEventsByDateFrame($select, $startDate, $endDate);
            $this->getEventListByEventTypesRestrict($daoEvent, $select, $key, $startDate, $endDate, $limit);
            $r = $select->limit($limit)->query()->fetchAll();
            $tmp['rows'] = $r;
            if (empty($r)) {
                $emptyList[] = $tmp;
            } else {
                $list[] = $tmp;
            }
        }
        foreach ($emptyList as $tmp) {
            $list[] = $tmp;
        }
        return $list;
    }

    protected function getEventListByEventTypesRestrict(Application_Model_DAO_Event $daoEvent, Zend_Db_Table_Select $select, $infoType, $startDate, $endDate, $limit = 10) {
        // @extension point
    }

    protected function getStartAndEndDates() {
        $defaultDayOffset = 3;  // @todo config
        $startDate = $this->_getParam('start_date');
        if ($startDate == null) {
            $startDate = date(My_Db_Table::DATE_PATTERN);
        }
        $endDate = $this->_getParam('end_date');
        if ($endDate == null) {
            $tmp = explode('-', $startDate);
            $endDate = date(My_Db_Table::DATE_PATTERN, mktime(0, 0, 0, $tmp[1], $tmp[2], $tmp[0]) + $defaultDayOffset * 24 * 60 * 60);
        }
        $this->view->startDate = $startDate;
        $this->view->endDate = $endDate;
        return array($startDate, $endDate);
    }

}
